import React from "react";
import { DataAction, IProTableRef } from "vendor/components/pro-table-native";
import { getAccountPageList, getAccountInfo, createAccount, updateAccount, deleteAccount } from "../../api";

export const getDataAction = (proTableRef: React.MutableRefObject<IProTableRef>): DataAction => ({
  fetchList: (params) => {
    const { page, pageSize, filter } = params;
    return getAccountPageList({ page, pageSize, ...filter }).then((resp) => resp.data)
  },
  getItem: (rowKey) => {
    const id = rowKey as number;
    return getAccountInfo(id).then((resp) => {
      return {
        name: resp.data.name,
        agentId: resp.data.agentId,
        address: resp.data.address,
        linkman: resp.data.linkman,
        linkmanPhone: resp.data.linkmanPhone,
        status: resp.data.status,
        remark: resp.data.remark,
      };
    })
  },
  createItem: (params) => {
    return createAccount(params).then((resp) => {
      proTableRef.current.doFetchList();
      return resp.data
    })
  },
  updateItem: (rowKey, params) => {
    return updateAccount(rowKey as number, params).then((resp) => {
      proTableRef.current.doFetchList();
      return resp.data
    })
  },
  deleteItems: (selectedRowKeys) => {
    return deleteAccount(selectedRowKeys.map(item => parseInt(item)))
  },
})


